<?php
/**
 * @file name : project_model.php
 * @package   :
 * @author    : Vo Thanh Minh
 * @email     : <thanhminhld@yahoo.com - thanhminhld@gmail.com>
 * @copyright : 2011
 *
 */
if (!defined('BASEPATH')) {
    exit('No direct script access');
}

class Project_model extends Base_Model
{
    /**
     * @function __construct
     * @author   Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     */
    public function __construct()
    {
        parent::__construct();
        $this->_tbl       = 'projects';
    }
    /**
     * getTotalRows
     *
     * @author Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access public
     * @param array $arrWhere
     *
     * @return int
     */
    public function getTotalRows($arrWhere = array(), $field_groupby = '')
    {
        if (count($arrWhere) > 0) {
            $this->db->where($arrWhere);
        }
        if (!empty($field_groupby) && is_string($field_groupby)) {
            if ($this->db->field_exists($field_groupby, $this->_tbl)) {
                $this->db->group_by($field_groupby);
            }
        }
        $query = $this->db->get_where($this->_tbl, $arrWhere);

        return $query->num_rows();
    }
    /**
     * @function loadList
     *
     * @author Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access public
     * @return array
     */
    public function loadList($arrWhere = array(), $limit = 10, $offset = 0)
    {
        if ($limit == 'all') {
            $query = $this->db->get_where($this->_tbl, $arrWhere);
        } else {
            $query = $this->db->get_where($this->_tbl, $arrWhere, $limit, $offset);
        }
        $arrList = array();
        if ($query->num_rows() > 0) {
            foreach($query->result() as $row) {
                $arrList[] = $row;
            }
        }
        return $arrList;
    }
    /**
     * @function copyFileFolder
     * @author   Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access   private
     * @param     string
     * @param     boolean
     */
    public function setPermission(&$errCode, $path, $resource, $option = '', $mode = '')
    {
        if (file_exists($path . '/' . $resource)) {
            $outPut = array();
            exec('chmod ' . $option . ' ' . $mode . ' ' . $path . '/' . $resource, $outPut);
            if (count($outPut) > 0) {
                foreach ($outPut as $k => $err) {
                    $this->messages->add($err, 'error');
                }
                $errCode = TRUE;
                return FALSE;
            }
            return TRUE;
        }
        return FALSE;
    }
}